import java.rmi.server.UnicastRemoteObject ;
import java.rmi.RemoteException ;
import java.util.Hashtable;
import java.util.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;


public class Serveur_Impl extends UnicastRemoteObject implements Serveur_Itf  
{    
	Hashtable listeClients = new Hashtable();
	int idProchainClient;
	String sender;
	JFrame fenetre;
	JButton lister;
	JButton quitter;	
	JPanel body;	
	JPanel button;
	JPanel screen;
	JPanel pLister;
	JPanel pQuitter;
	JTextArea textEcran;

	public Serveur_Impl () throws RemoteException 
	{
		super();
		initFenetre();
		fenetre.setVisible(true);	
		textEcran.append("\nServeur prêt.");
		idProchainClient = 0;	
	}


	public int addList(Client_Itf c) throws RemoteException 
	{ 
		idProchainClient++;
		String key = "" + idProchainClient;
		
		listeClients.put(key, c);
		textEcran.append("\nAjout du client " + key);
		
		return idProchainClient;
	}


	public void getVoisins(int id) throws RemoteException
	{
		int indice = -1;
		int nbVoisins = 0;

		Hashtable sesVoisins = new Hashtable();										// liste des voisins du client
		Client_Itf c = (Client_Itf) listeClients.get("" + id);
		
		if(listeClients.size() > 1)													// s'il y a au moins deux clients
		{			
			nbVoisins = ((int) (Math.random()*(listeClients.size()-1)))+1; 			// entre [0;listeClientSize-1[ + 1 = entre [1;listeCientSize-1]
			
			textEcran.append("\n\tNombre de voisins attribués : " + nbVoisins);

			for(int i=0; i<nbVoisins; i++)
			{
				do
				{
					indice = (int) (Math.random()*(listeClients.size()-1) +1);		// indice du client à ajouter dans les voisins de c
				}
				while(indice == id || sesVoisins.containsKey(indice)); 				// tant que c'est lui-meme ou qqun qu'il a déjà
			

				sesVoisins.put(indice, indice);
			
				Client_Itf cl = (Client_Itf)listeClients.get("" + indice);
			
				c.addVoisinConnection(cl, indice);
			}
		}
	}
	
	public void iSend(String ip, String port) throws RemoteException
	{
		sender = ip + "/" + port;
	}
	
	public String iReceive() throws RemoteException
	{
		return sender;
	}
	
	public void initFenetre()
	{			
		fenetre = new JFrame();
		fenetre.setResizable(false); 
		fenetre.setTitle("Serveur");
		fenetre.setFont(new java.awt.Font("Bookman Old Style",0,14));
		fenetre.setLocationRelativeTo(null);
		fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		fenetre.setSize(new Dimension(520, 420));
	
		lister = new JButton("Lister clients");
		quitter = new JButton("Quitter");
		body = new JPanel();
		textEcran = new JTextArea();
		button = new JPanel();
		screen = new JPanel();
		pLister = new JPanel();
		pQuitter = new JPanel();
		
		GridLayout glButton = new GridLayout(2,1);
		button.setLayout(glButton);
		GridLayout glBoddy = new GridLayout(1,2);
		body.setLayout(glBoddy);
		GridLayout glScreen = new GridLayout(1,1);
		screen.setLayout(glScreen);
		
		lister.addActionListener(new ListerListener());
		quitter.addActionListener(new QuitterListener());
		
		fenetre.add(body);
		pLister.add(lister);
		pQuitter.add(quitter);
		button.add(pLister);
		button.add(pQuitter);
		screen.add(textEcran);
		body.add(screen);
		body.add(button);
		screen.add(textEcran);
		
		textEcran.append("");
        textEcran.setEnabled(false);
        textEcran.setLineWrap(true);		// retour a la ligne
        textEcran.setForeground(Color.magenta);
        textEcran.setFont(new java.awt.Font("Verdana",0,14));
        textEcran.setDisabledTextColor(new java.awt.Color(50,85,100));
        textEcran.setOpaque(true);			
        textEcran.setBackground(Color.WHITE);
        textEcran.setAlignmentY(SwingConstants.TOP);
	}
	
	public class ListerListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0) 
		{   
			textEcran.append("\n\n** Liste des clients connectés :");			
			for(Enumeration e = listeClients.keys(); e.hasMoreElements();)
			{
				String cle = (String)e.nextElement();
				textEcran.append("\n   Client " + cle);			
			}
		}
	}
	
	public class QuitterListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0) 
		{   
			fenetre.setVisible(false);
        	System.exit(0);
		}
	}
}

